var RolesModule = function () {
	
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	"autoWidth": false,
                    "scrollX": true,
                	"lengthMenu": [
                        [10, 15, 20, 50, -1],
                        [10, 15, 20, 50, "All"]
                    ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    'sortable': false,
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "",
						    "targets": [0]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "-",
						    "targets": [1]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "-",
						    "targets": [2]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "-",
						    "targets": [3]
						}
						
                    ],
                    "columns": [
                        { "data": "typeName" },
                        { "data": "approveRoleNum" },
                        { "data": "employeeId" },
                        { "data": "operater" }
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/approveRoleGroup/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   $('td:eq(0)', nRow).html(aData.typeName);
                	   $('td:eq(1)', nRow).html(aData.approveRoleNum);
                	   $('td:eq(2)', nRow).html(aData.employeeName);
                	   if("项目最终审批人" == aData.typeName){
                		   $('td:eq(3)', nRow).html('<a class="addlast fa fa-child  fa-lg " title="添加终审审批人" href="javascript:;"></a>');  
                	   }else{
                		   $('td:eq(3)', nRow).html('<a class="detail fa fa-bars fa-lg " title="查看" href="javascript:;"></a>');  
                	   }
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "{";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"page":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   json=json+"}";
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                
                // 查看详情
                $('#modelEditable').on("click", "a.detail", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx +"/approveRoleGroup/detail/"+id;
                    return;
                }));
                // 添加终审审批人
                $('#modelEditable').on("click", "a.addlast", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    $("#approveGroupId").val(id);
                    $("#btn_allot_employee").click();
                    return;
                }));
            }
        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();


$('#btn_add_group').click(function(){
	var approveName = $('#group_name').val();
	if(approveName.length >10 || approveName.length <=0){
		alert("名称输入长度有误.")
		return;
	}
	json = "{\"approveName\":\""+approveName+"\"}";
    var str = json;
    $.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/approveRoleGroup/addGroup",
        data:json,
        dataType:"json",
        success: function(msg){
        	$('#dsimiss_add_relation').click();
        	if(msg.code == 1){
        		showTips(msg.message,3000,true);
        	}else if(msg.code == 0){
        		showError(msg.message,3000,true);
        	}
        },
        error: function (msg) {
        	bootbox.alert(msg.message);
        }
    });
	
});

//Autocomplete
$("#tags").autocomplete({
    source: function(query,process){
        $.post(ctx + "/investor/employee/"+query.term,{"matchCount":200},function(data){
        	var ebean = []
        	for (var int = 0; int < data.length; int++) {
        		var temp = [];
        		temp.label = data[int].employeeName +"  "+ data[int].employeeNo;
        		temp.value = data[int].employeeNo;
        		temp.id = data[int].employeeId;
        		temp.name = data[int].employeeName;
        		ebean.push(temp);
    		}
            return process(ebean);
        });
    },
    search:function(event, ui){
    	$("#employeeName").html('');
        $("#employeeNo").html('');
        $("#employeeId").val('');
    },
    select: function(event, ui) {
        $("#tags").val(ui.item.label);
        $("#employeeName").html(ui.item.name);
        $("#employeeNo").html(ui.item.value);
        $("#employeeId").val(ui.item.id);
        var uistr =ui.item;
        // 阻止事件的默认行为
        event.preventDefault(); 
    }
});

$("#btn_add_emp_last").click(function(){
	var employeeId = $('#employeeId').val();
	var employeeName = $('#employeeName').html();
	var groupId = $('#approveGroupId').val();
	if (employeeId == null || employeeId == '') {
		bootbox.alert("请选择理财师！");
		return;
	}
	json = "{\"employeeId\":\""+employeeId+"\",\"groupId\":\""+groupId+"\"}";
	$.ajax({
    	type:"post",
        contentType:"application/json; charset=UTF-8",
        url: ctx + "/approveRoleGroup/allotLastEmp",
        data:json,
        dataType:"json",
        success: function(msg){
        	$.teninedialog({
                title:'系统提示',
                content:msg.message,
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	if(msg.code == 2){
                		$(this).closeDialog(modal);
                	}else{
                		location.reload();
                	}
                }
            });
        },
        error: function (msg) {
        	bootbox.alert("服务器偷了个小懒,分配理财师失败了!");
        }
    });
});